package com.guomao.entity;

import com.guomao.utils.*;
import org.lwjgl.Sys;

import java.io.IOException;

public class Tank extends Element {


    int surplus = 0;

    public int distance = 16;

    private int num = 16;

    public Direction default_dir = Direction.UP;
    public Direction unmoved_dir;

    public Tank() {

    }

    public Tank(int x, int y) {
        this.x = x;
        this.y = y;
        super.path = "day15-javase-tank\\images\\tank_u.gif";
        getSize();
    }

    public void move(Direction d) {
        if (unmoved_dir != null && unmoved_dir == d) {
            switch (d) {
                case UP:
                    y -= surplus;
                    break;

                case DOWN:
                    y += surplus;
                    break;

                case LEFT:
                    x -= surplus;
                    break;

                case RIGHT:
                    x += surplus;
                    break;
            }
            return;
        }

        if (d == default_dir) {
            switch (d) {
                case UP:
                    y -= distance;
                    break;
                case DOWN:
                    y += distance;
                    break;
                case LEFT:
                    x -= distance;
                    break;
                case RIGHT:
                    x += distance;
                    break;
            }
        } else {
            switch (d) {
                case UP:
                    this.path = "day15-javase-tank\\images\\tank_u.gif";
                    break;
                case DOWN:
                    this.path = "day15-javase-tank\\images\\tank_d.gif";
                    break;
                case LEFT:
                    this.path = "day15-javase-tank\\images\\tank_l.gif";
                    break;
                case RIGHT:
                    this.path = "day15-javase-tank\\images\\tank_r.gif";
                    break;
            }

            this.default_dir = d;
        }

        if (x < 0) x = 0;
        if (y < 0) y = 0;
        if (x > Meta.WIDTH - this.width) x = Meta.WIDTH - this.width;
        if (y > Meta.HEIGHT - this.height) y = Meta.HEIGHT - this.height;
    }

    long gap = 500;
    long lastFireTime;

    public Bullet fire() {
        Bullet bullet = null;
        long currentFireTime = System.currentTimeMillis();
        if (currentFireTime - lastFireTime >= gap) {
            try {
                SoundUtils.play("day16-javase-tank\\audio\\fire.wav");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            bullet = new Bullet(x, y, width, height, default_dir);
            lastFireTime = currentFireTime;
        }
        return bullet;
//        System.out.println("坦克的开火方法执行!");
        //x y 是坦克的坐标
        //w h 传入坦克的宽高
        //Direction d 传入坦克的方向
    }


    public boolean isCollide(Element e) {
        int x1 = this.x;
        int y1 = this.y;
        int w1 = this.width;
        int h1 = this.height;

        switch (default_dir) {
            case UP:
                y1 -= num;
                break;

            case DOWN:
                y1 += num;
                break;

            case LEFT:
                x1 -= num;
                break;

            case RIGHT:
                x1 += num;
                break;
        }

        int x2 = e.x;
        int y2 = e.y;
        int w2 = e.width;
        int h2 = e.height;
        boolean flag = CollisionUtils.isCollisionWithRect(x1, y1, w1, h1, x2, y2, w2, h2);

        if (flag) {
            unmoved_dir = default_dir;
            switch (default_dir) {
                case UP:
                    surplus = super.y - y2 - h2;
                    break;
                case DOWN:
                    surplus = y2 - super.y - h1;
                    break;
                case LEFT:
                    surplus = super.x - x2 - w2;
                    break;
                case RIGHT:
                    surplus = x2 - super.x - w1;
                    break;
            }
        } else {
            unmoved_dir = null;
        }
        return flag;
    }


/* version 1
public boolean isCollide(Element e) {
        int x1 = this.x;
        int y1 = this.y;
        int w1 = this.width;
        int h1 = this.height;

        int x2 = e.x;
        int y2 = e.y;
        int w2 = e.width;
        int h2 = e.height;
        boolean flag = CollisionUtils.isCollisionWithRect(x1, y1, w1, h1, x2, y2, w2, h2);

        if (flag) {
            //撞墙version1:修正坦克的坐标
            switch (default_dir) {
                case UP:
                    this.y = y2 + h2;
                    break;
                case DOWN:
                    this.y = y2 - h1;
                    break;
                case LEFT:
                    this.x = x2 + w2;
                    break;
                case RIGHT:
                    this.x = x2 - w1;
                    break;
            }
        }
        return flag;
    }*/
}
